package com.pzh.vue.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzh.vue.mapper.RoleMenuMapper;
import com.pzh.vue.pojo.Role;
import com.pzh.vue.mapper.RoleMapper;
import com.pzh.vue.pojo.RoleMenu;
import com.pzh.vue.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-03-27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    //事务控制
    @Transactional
    @Override
    public void setRoleMenu(Integer roleId, List<Integer> menuId) {
        QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        //先删在存
        roleMenuMapper.delete(queryWrapper);
        for (Integer mid:menuId){
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(mid);
            roleMenuMapper.insert(roleMenu);
        }

    }
}
